<?php

/**
 * 用户管理
 * @author  Mr.L <349865361@qq.com>
 */

namespace app\topics\admin;

class UserAdmin extends \app\system\admin\SystemExtendAdmin {

    protected $_model = 'TopicsUser';

    /**
     * 模块信息
     */
    protected function _infoModule() {
        return [
            'info' => [
                'name' => '用户管理',
                'description' => '管理论坛用户',
            ],
            'fun' => [
                'index' => true,
                'add' => true,
                'edit' => true,
                'del' => true,
                'status' => true,
            ]
        ];
    }

    public function _indexParam() {
        return [
            'keyword' => 'A.username'
        ];
    }

    public function _indexOrder() {
        return 'user_id desc';
    }

    protected function _addAssign() {
        return array(
            'roleList' =>target('topics/TopicsUserRole')->loadList(),
        );
    }

    protected function _editAssign($info) {
        return array(
            'roleList' => target('topics/TopicsUserRole')->loadList(),
        );
    }

    protected function _delBefore($id) {
        if ($id == 1) {
            $this->error('保留用户无法删除！');
        }
    }

    public function ajaxList() {
        $keyword = request('get', 'q', '', 'html_clear');
        $list = target($this->_model)->loadList(['_sql' => 'A.username like "%'.$keyword.'%"']);
        foreach ($list as $key => $vo) {
            $desc = [];
            $list[$key]['id'] = $vo['user_id'];
            $list[$key]['text'] = $vo['username'];
            $list[$key]['image'] = $vo['avatar'];
            $list[$key]['desc'] =  $vo['role_name'];
        }

        $this->json([
            'items' => $list,
            'total_count' => count($list),
            'incomplete_results' => false
        ]);
    }

}